/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package magicthegathering;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.LinkedList;
import java.util.*;
import java.util.Collections;
import javax.imageio.ImageIO;
/**
 *
 * @author Fluffy & Chronor
 */
public class MTG_Model {
Player player1;
Player player2;
List<Card> CardsInPlay1;
List<Card> CardsInPlay2;
List<MTG_View> observers = new LinkedList<MTG_View>();

public  MTG_Model() {

    player1 = new Player();
    player2 = new Player();
    //New Game - lets get it going!
    player1 = new Player();
    player1.life = 20;
    player1.hand = 7;
    player1.deck = 33;
    player1.red = 0;
    player1.green = 0;
    player1.blue = 0;
    player1.white = 0;
    player1.black = 0;


    //Player 2
    player2 = new Player();
    player2.life = 20;
    player2.hand = 7;
    player2.deck = 33;
    player2.red = 0;
    player2.green = 0;
    player2.blue = 0;
    player2.white = 0;
    player2.black = 0;

     //Need to add *deck randomization and *draw card rules here.
    player1.hand_classes = new ArrayList();
    player1.deck_classes = new ArrayList();
    player2.hand_classes = new ArrayList();
    player2.deck_classes = new ArrayList();


    //Temporary for 1st milestone
    //Activate the cards in play
    CardsInPlay1 = new ArrayList();
    CardsInPlay2 = new ArrayList();



    //Deckmake
    DeckMake("GreenDeck.txt", 0);
    DeckMake("BlackDeck.txt", 1);

}
//senseless Functions go here. Since there's nowehere else to place them.
void CardsInPlay1AddCard(Card card){
    CardsInPlay1.add(card);
}
void CardsInPlay1removeCard(int i){
    CardsInPlay1.remove(i);
}
void CardsInPlay2AddCard(Card card){
    CardsInPlay2.add(card);
}
void CardsInPlay2removeCard(int i){
    CardsInPlay2.remove(i);
}
void player1AddCardHand(Card card){
    player1.hand_classes.add(card);
}
void player1removeCardHand(int i){
    player1.hand_classes.remove(i);
}
void player2AddCardHand(Card card){
    player2.hand_classes.add(card);
}
void player2removeCardHand(int i){
    player2.hand_classes.remove(i);
}
void player1AddCardDeck(Card card){
    player1.deck_classes.add(card);
}
void player1removeCardDeck(int i){
    player1.deck_classes.remove(i);
}
void player2AddCardDeck(Card card){
    player2.deck_classes.add(card);
}
void player2removeCardDeck(int i){
    player2.deck_classes.remove(i);
}

void DeckMake(String filename, int player) {
            int ID = 1;
            boolean color = false; //false=black | true=green
            Card card = new Card();
            MTG_Functions mtg_func = new MTG_Functions();
            List<String> Player1_Deck = mtg_func.readfile(filename);
            int cardcount = 0;
            for(int i = 1; i < Player1_Deck.size(); i++) {
            String temp = Player1_Deck.get(i);
            List<String> temp_array = new ArrayList();
            File dir1 = new File (".");
            String filepath = "";
            try {
                filepath = dir1.getCanonicalPath();
            }
            catch(Exception e) {
                e.printStackTrace();
            }

            StringTokenizer tokens = new StringTokenizer(temp, " ");
            while(tokens.hasMoreTokens()) {
                temp_array.add(tokens.nextToken());
            }
            for(int j = 0; j < Integer.parseInt(temp_array.get(0)); j++) {
                if(temp_array.get(2).equals("Land")) {
                    card = new Card();
                    if(card.name=="Swamp") color=false;
                    else color=true;
                    card.type = temp_array.get(2);
                    card.name = temp_array.get(1);
                }
                else if(temp_array.get(2).equals("Creature")) {
                    card = new Card();
                    card.type = temp_array.get(2);
                    card.name = temp_array.get(1);
                    card.attack =Integer.parseInt( temp_array.get(4));
                    card.attack_tmp = card.attack;
                    card.defense = Integer.parseInt(temp_array.get(5));
                    card.defense_tmp = card.defense;
                    card.costcolor = Integer.parseInt(temp_array.get(3));
                    card.block = -1;
                    String cfile;
                    if(!color) cfile = filepath + File.separator +"snake.jpg";
                    else cfile = filepath + File.separator +"creature.png";
                    try {
                        card.img = ImageIO.read(new File(cfile));
                    } catch(Exception e) {
                        System.out.println("Could not find creature file");
                    }
                }
                else if(temp_array.get(2).equals("Spell")) {
                    card = new Card();
                    card.type = temp_array.get(2);
                    card.name = temp_array.get(1);
                    card.costcolor = Integer.parseInt(temp_array.get(3));
                    card.spell_type = temp_array.get(4);
                    card.attack = Integer.parseInt(temp_array.get(5));
                    card.attack_tmp = card.attack;

                    //Add abilities later.... yeah....
                    String cfile = filepath + File.separator + "spell.jpg";
                    try {
                        card.img = ImageIO.read(new File(cfile));
                    } catch(Exception e) {

                        System.out.println("Could not find spell file");
                    }

                }
                card.unitID = ID;
                if(player == 0){
                    player1.deck_classes.add(card);
                }
                if(player == 1){
                    player2.deck_classes.add(card);
                }
                cardcount++;
                ID++;
            }
        }
         //Grab deck type
            String temp2 = Player1_Deck.get(0);
            List<String> temp_array2 = new ArrayList();
            StringTokenizer tokens2 = new StringTokenizer(temp2, " ");
            while(tokens2.hasMoreTokens()) {
                temp_array2.add(tokens2.nextToken());
            }

        //Shuffle and draw hand!
        if(player == 0){
            player1.deck_type = temp_array2.get(1);
            Shuffle(player1.deck_classes);
            Draw(7, player1.deck_classes, player1.hand_classes);
        }
        if(player == 1) {
            player2.deck_type = temp_array2.get(1);
            Shuffle(player2.deck_classes);
            Draw(7, player2.deck_classes, player2.hand_classes);

        }

}
class Player {
    int life;
    int hand;
    int deck;
    String deck_type; //Type of deck, green, black, red... etc
    int red;
    int green;
    int blue;
    int white;
    int black;
    int red_tmp;
    int green_tmp;
    int blue_tmp;
    int white_tmp;
    int black_tmp;

    int getLife() {
        return life;
    }
    void setLife(int i){
        life = i;
    }

    int getRed_tmp() {
        return red_tmp;
    }
    void setRed_tmp(int i){
        red_tmp = i;
    }
    int getGreen_tmp() {
        return green_tmp;
    }
    void setGreen_tmp(int i){
        green_tmp = i;
    }
    int getBlue_tmp() {
        return blue_tmp;
    }
    void setBlue_tmp(int i){
        blue_tmp = i;
    }
    int getWhite_tmp() {
        return white_tmp;
    }
    void setWhite_tmp(int i){
        white_tmp = i;
    }
    int getBlack_tmp() {
        return black_tmp;
    }
    void setBlack_tmp(int i){
        black_tmp = i;
    }
    void setHand(int i){
        hand = i;
    }
    int getHand(){
        return hand;
    }
    void setDeck(int i){
        deck = i;
    }
    int getDeck(){
        return deck;
    }

    List<Card> deck_classes; //what is left in the deck
    List<Card> hand_classes; //what is in the hand
}

class Card {
   //Holds valuable Card information
    String name;
    int defense;
    int attack;
    int attack_tmp, defense_tmp;
    String type;
    String color_type;
    int order; //order in deck or ID #
    String picture; //path
    String description;
    String spell_type; //Spell damage type
    int xpos, ypos; //Position of card on the table
    int costcolor, costother; //Cost to put into play
    int sickness = 1;
    int status = 0; //0 = neutral, 1 = attacking, 2 = blocking
    int block = -1; //determines which card to block or not
    BufferedImage img;
    int unitID; //individual id for cards
    int BlockingID;
    //Get/Set Methods
    public int getBlock(){
        return block;
    }
    public void setBlock(int i){
        block = i;
    }
    public int getStatus(){
        return status;
    }
    public void setStatus(int i){
        status = i;
    }
    public int getAttack_tmp() {
        return attack_tmp;
    }
    public void setAttack_tmp(int i) {
        attack_tmp = i;
    }
    public int getDefense_tmp() {
        return defense_tmp;
    }
    public void setDefense_tmp(int i) {
        defense_tmp = i;
    }
    public int updatexpos(int x) {
        xpos = x;
        return x;
    }
    public int updateypos(int x) {
        ypos = x;
        return x;
    }

    public int getxpos(){
        return xpos;

    }
    public int getypos(){
        return ypos;

    }
    public int getSickness() {
        return sickness;
    }
    public void setSickness(int i) {
        sickness = i;
    }

}
public void setValue(int v) {
     int value = 0;
     for(MTG_View observer: observers){
         observer.valueChanged(value);
     }
     value = v;
 }

List<Card> Shuffle(List<Card> deck){
    java.util.Collections.shuffle(deck);
    return deck;
}
List<Card> Draw(int num, List<Card> deck, List<Card> hand){
    for(int i=0;i<num;i++){
        hand.add(deck.remove(0));
    }
    return hand;
}

 public void addObserver(MTG_View view){
     observers.add(view);
 }

}
